#include "i2c-board.h"
#include "ads1115-board.h"
#include "tremo_gpio.h"
#include "tremo_rcc.h"
#include "tremo_delay.h"
#include "system.h"


#if defined(CONFIG_DTU_AI)	

//配置ads1115
void BoardConfigeAds1115(uint16_t channelConfig)
{
	uint8_t i;
	uint8_t table[4] = {0};

	table[0] = 0x90; 	//CMD_Write Addr;
	table[1] = 0x01; 	//指向配置寄存器
	table[2] = (channelConfig & 0xFF00) >> 8;	//0xc2; 	//设置配置寄存器的高八位1100 0101，即AINp=AIN0，AINn=GND;满量程FS=+-4.096；连续转换模式
	table[3] = channelConfig & 0xFF;			//0xe3; 	//设置配置寄存器的低八位1100 0101，即

	IIC_Start(); 		//发送起始信号

	for(i = 0; i < 4; i++)
	{
		IIC_Send_Byte(table[i]); //将table里面的数写入ads1115
		IIC_Wait_Ack();
	}

	IIC_Stop(); 		//发送停止信号
}


uint16_t BoardAds1115Read(void)
{
	uint8_t data[2]={0};
	//转换指向寄存器
	IIC_Start();
	IIC_Send_Byte(0x90);
	IIC_Wait_Ack();
	IIC_Send_Byte(0x00);
	IIC_Wait_Ack();
	IIC_Stop();                        

	IIC_Start();
	IIC_Send_Byte(0x91);
	IIC_Wait_Ack();
	data[0] = IIC_Read_Byte(1);
	data[1] = IIC_Read_Byte(0);			//接收完成后，发送一个非ACK信号，以便产生stop信号
	IIC_Stop();

	return  ((uint16_t)(data[0]<<8 | data[1]));
}


//获取最终模数转换之后的结果
uint16_t BoardAds1115Result(uint16_t channelConfig)
{
	uint16_t value = 0;
	BoardConfigeAds1115(channelConfig);

	value = BoardAds1115Read();
	return value;
}


#endif


